<?php

/**
 * Controller_staff
 教师档案
 */

class Controller_staff extends MyApp {


	public function __construct()
	{
		parent::__construct();

		$this->model->staff   = YOU::loadModel('staff');
		$this->model->class   = YOU::loadModel('class');
		$this->model->subject = YOU::loadModel('subject');
		$this->model->semester = YOU::loadModel('semester');		
	}

	/** 首页 **/
	public function actionIndex()
	{

	}

	/** json列表 **/
	public function actionListJson()
	{
		$q     = $this->post->q;
		$limit = $this->get->limit;

		$sql = "SELECT staff_id,staff_name FROM `tbl_staff` WHERE staff_name LIKE '{$q}%'";

		if (!empty($limit)){
			$sql .= " LIMIT 0,{$limit} ";
		}

		$staff_lists = $this->db->fetchAll($sql);
		$data = array();
		foreach ($staff_lists as $key => $value){
			$data[$key]['id']   = $value['staff_id'];
			$data[$key]['name'] = $value['staff_name'];
		}

		echo json_encode($data);

	}

	public function actionSubjectJson()
	{
		// 教师列表
		$class = $this->model->class->getIni();

		// 教师列表
		$staff = $this->model->staff->getIni();

		// 学科列表
		$subject = $this->model->subject->getIni();

		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 本学期课程表所有记录
		$sb_lists = $this->db->fetchAll("SELECT * FROM `tbl_staff_subject` {$where}");

		$tree = array();
		foreach ($sb_lists as $key => $value){
			$c[$value['class_id']][$value['subject_id']][$value['staff_id']] = array("n" => $staff[$value['staff_id']], "v" => $value['staff_id']);
		}

		foreach ($c as $key => $value){

			$i = 0;
			foreach ($value as $k => $v){
				$bb[$i]['n'] = $subject[$k];
				$bb[$i]['v'] = $k;

				$ii = 0;
				foreach ($v as $t => $w){

					$bb[$i]['s'][$ii] = $w;
					$ii++;
				}
				$i++;
			}

			$s = array(
			"n" => $class[$key],
			"v" => $key,
			"s" => $bb,
			);
			$tree[] = $s;

		}

		echo json_encode($tree);
	}

	/** 剩余课时 **/
	public function actionHourJson()
	{
		$id    = $this->get->id;
		$count = $this->get->count;

		$data = $this->db->fetchRow("SELECT * FROM `tbl_staff_subject` WHERE id='{$id}' ");
		if (empty($data)){
			exit();
		}

		$row = $this->model->staff->getInfo($data['staff_id']);
		$staff_name   = $row['staff_name'];
		$row =  $this->model->subject->getInfo($data['subject_id']);
		$subject_name = $row['subject_name'];


		// 当前剩余课时
		$week_now = $data['week_hour'] - ($data['hour_count']+$count);

		$html = "{$staff_name} - {$subject_name}（{$week_now}）";

		echo $html;
		exit();

	}

	/** 科任指派列表 **/
	public function actionSubjectList()
	{
		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];


		$staff_name = $this->get->staff_name;
		$class_id   = $this->get->class_id;
		$subject_id = $this->get->subject_id;

		$where = " WHERE 1=1 AND semester_id='{$semester_id}'";

		if (!empty($staff_name)){
			$where .= " AND b.staff_name LIKE '{$staff_name}%' ";
			$this->tpl->assign("staff_name", $staff_name);
		}
		if (!empty($class_id)){
			$where .= " AND a.class_id = '{$class_id}' ";
			$this->tpl->assign("class_id", $class_id);
		}
		if (!empty($subject_id)){
			$where .= " AND a.subject_id = '{$subject_id}' ";
			$this->tpl->assign("subject_id", $subject_id);
		}

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_staff_subject` AS a,`tbl_staff` AS b {$where} AND a.staff_id=b.staff_id");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$sb_lists = $this->db->fetchAll("SELECT a.*,b.staff_name FROM `tbl_staff_subject` AS a,`tbl_staff` AS b  {$where} AND a.staff_id=b.staff_id ORDER BY id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($sb_lists as $key => $value){
			$row = $this->model->class->getInfo($value['class_id']);
			$sb_lists[$key]['class_name']   = $row['class_name'];
			$row = $this->model->subject->getInfo($value['subject_id']);
			$sb_lists[$key]['subject_name'] = $row['subject_name'];
		}

		// 班级列表
		$class_lists = $this->model->class->getAll();
		// 学科列表
		$subject_lists = $this->model->subject->getAll();

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("count", $count);
		$this->tpl->assign("sb_lists", $sb_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->display("staff/staff.subjectlist.tpl.php");
	}


	/** 科任指派修改 **/
	public function actionSubjectUpdate()
	{
		$id = (int)$this->get->id;
		$sb_data = $this->db->fetchRow("SELECT * FROM `tbl_staff_subject` WHERE id='{$id}'");
		if (empty($sb_data)){
			Helper_Client::alert("数据不存在");
		}
		if ($_POST){
			$week_hour = $this->post->week_hour;

			$info = array(
			"week_hour" => $week_hour,
			"update_time" => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_staff_subject", $info, "id='{$id}'");

			Helper_Client::location("修改成功", "index.php?a=staff.subjectlist");
		}

		// 教师名称
		$row = $this->model->staff->getinfo($sb_data['staff_id']);
		$sb_data['staff_name'] = $row['staff_name'];

		// 年级名称 班级名称
		$row = $this->model->class->getGinfo($sb_data['class_id']);
		$sb_data['grade_name'] = $row['grade_name'];
		$sb_data['class_name'] = $row['class_name'];
		// 学科名称
		$row = $this->model->subject->getInfo($sb_data['subject_id']);
		$sb_data['subject_name'] = $row['subject_name'];

		$this->tpl->assign("sb_data", $sb_data);
		$this->tpl->display("staff/staff.subjectupdate.tpl.php");
	}

	/** 科任指派批量添加 **/
	public function actionSubjectAdd()
	{
		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		if ($_POST){
			$staff_id    = (int)$this->post->staff_id;
			$class_ids   = $this->post->class_ids;
			$subject_ids = $this->post->subject_ids;
			$week_hour   = $this->post->week_hour;

			$staff_data = $this->model->staff->getInfo($staff_id);
			if (empty($staff_data)){
				Helper_Client::alert("教师信息不存在");
			}

			// 写入staff所有的任课指派
			foreach ($class_ids as $class_id){
				foreach ($subject_ids  as $subject_id){
					// 批量写入
					$info = array(
					"staff_id"    => $staff_id,
					"class_id"    => $class_id,
					"subject_id"  => $subject_id,
					"semester_id" => $semester_id,
					"week_hour"   => $week_hour,
					"status"      => 1,
					"create_time" => date("Y-m-d H:i:s"),
					"update_time" => "0000-00-00 00:00:00",
					);
					$this->db->do_insert("tbl_staff_subject", $info);

				}
			}

			// 跳转
			Helper_Client::location("指派成功", "index.php?a=staff.subjectlist");
		}


		$subject_lists = $this->model->subject->getAll();
		$class_lists   = $this->model->class->getAll();

		// 员工列表给下拉
		$staff_lists   = $this->model->staff->getAll();

		$this->tpl->assign("week_hour", $week_hour);
		$this->tpl->assign("staff_data", $staff_data);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->display("staff/staff.subjectadd.tpl.php");

	}

	/** 删除科任指派 **/
	public function actionSubjectDel()
	{
		$id = $this->get->id;

		$sql = "DELETE FROM `tbl_staff_subject` WHERE id='{$id}'";
		$this->db->query($sql);

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=staff.subjectlist");

	}


}

?>